using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace Game.UI {
	public class UIWindow : MonoBehaviour, IPointerClickHandler {
		public WindowType WindowType;
		public Button CloseButton;
		public Vector2 MinSize = new Vector2(500, 500);
		public bool Resizable = true;
		public bool Draggable = true;

		RectTransform _rt;
		Canvas _canvas;
		protected UIHolder _owner;

		public RectTransform RectTransform => _rt;
		public Canvas CanvasCanvas => _canvas;

		public void OnPointerClick(PointerEventData eventData) {
			_owner.MoveWindowToTop(this);
			_owner.HasFocus= true;
		}

		protected virtual void Awake() {
			_canvas = GetComponent<Canvas>();
			_rt= GetComponent<RectTransform>();
			_owner = GetComponentInParent<UIHolder>();
			_owner.AddWindow(this);
			if ( CloseButton!= null ) {
				CloseButton.onClick.AddListener(HideWindow);
			}
		}

		protected virtual void OnDestroy() {

		}

		public virtual void HideWindow() {
			_owner?.HideWindow(WindowType);
		}

		public void ShowWindow() {
			_owner?.ShowWindow(WindowType);
		}

		public virtual void OnWindowShow() {

		}
	}

}
